﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace NG
{
    class QAskedCriteriaHandler:IQAskedCriteriaHandler
    {
        private string tableName = "[dbo].[QAskedCriteria]";

        public void addAskedCriteria(QAskedCriteria askedCrit)
        {
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;
            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }
            string query = string.Format("INSERT INTO" + tbn + "(questionNo,criteriaNo,criteriaDesc)VALUES("
                + "{0},{1},'{2}');", askedCrit.questionNo, askedCrit.criteriaNo, askedCrit.criteriaDesc);
               

            SqlCommand cmd = new SqlCommand(query, con.connection);
            cmd.ExecuteNonQuery();
            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
        }

        public void updateAskedCriteria(QAskedCriteria askedCrit, QAskedCriteria naskedCrit)
        {
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;
            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }
            string query = string.Format("UPDATE" + tbn + " SET criteriaDesc= '{2}"
                + "WHERE  questionNo = {0} AND CriteriaNo = {1} ,",askedCrit.questionNo,askedCrit.criteriaNo,askedCrit.criteriaDesc);
            SqlCommand cmd = new SqlCommand(query, con.connection);
            cmd.ExecuteNonQuery();
            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
        }

        public void deleteAskedCriteria(QAskedCriteria Crit)
        {
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;

            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }

            string query = string.Format("DELETE FROM " + tbn + " WHERE questionNo = {0} AND criteriaNo = {1} ", Crit.questionNo, Crit.criteriaNo);
            SqlCommand cmd = new SqlCommand(query, con.connection);
            cmd.ExecuteNonQuery();

            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
        }

        public LinkedList<QAskedCriteria> allAskedCriteria(string sortedBy, bool desc)
        {
            string sorted;
            if (sortedBy == string.Empty)
            {
                sorted = "{0}";
            }
            else
            {
                sorted = sortedBy;
            }
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;
            LinkedList<QAskedCriteria> qAskedCritList = new LinkedList<QAskedCriteria>();
            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }
            string extra = "";
            if (desc)
            {
                extra = " DESC";
            }

            string query = "SELECT * FROM " + tbn + "ORDER BY " + sorted + extra;
            SqlCommand cmd = new SqlCommand(query, con.connection);
            SqlDataReader rdr = cmd.ExecuteReader();


            if (rdr.HasRows)
            {

                while (rdr.Read())
                {

                    QAskedCriteria inter = new QAskedCriteria(int.Parse(rdr["questionNo"].ToString()),
                            int.Parse(rdr["criteriaNo"].ToString()), rdr["criteriaDesc"].ToString());


                    qAskedCritList.AddLast(inter);


                }
            }
            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
            return qAskedCritList;
        }
    }
}
